package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsMapper;
import com.atguigu.jxc.dao.PurchaseMapper;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Override
    public List<Supplier> supplierComboboxList(String q) {
        return purchaseMapper.supplierComboboxList(q);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void savePurchaseListGoods(PurchaseList purchaseList, List<PurchaseListGoods> list) {

        try {
            purchaseMapper.savePurchaseList(purchaseList);
            PurchaseList purchaseList1 = purchaseMapper.getPurchaseListByPNumber(purchaseList.getPurchaseNumber());
            ArrayList<Goods> goodss = new ArrayList<>();
            if (null!=purchaseList){
                for (PurchaseListGoods purchaseListGoods : list) {
                    purchaseListGoods.setPurchaseListId(purchaseList1.getPurchaseListId());
                    Goods goods1 = new Goods();
                    goods1.setGoodsId(purchaseListGoods.getGoodsId());
                    goods1.setInventoryQuantity(purchaseListGoods.getGoodsNum());
                    goodss.add(goods1);
                }
                purchaseMapper.savePurchaseListGoods(list);
                goodsMapper.addStockList(goodss);


            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveReturnListGoods(ReturnList returnList, List<ReturnListGoods> list) {
        try {
            purchaseMapper.saveReturnList(returnList);
            ReturnList returnList1 = purchaseMapper.getReturnListByPNumber(returnList.getReturnNumber());
            ArrayList<Goods> goodss = new ArrayList<>();
            if (null!=returnList1){
                for (ReturnListGoods returnListGoods : list) {
                    returnListGoods.setReturnListId(returnList1.getReturnListId());
                    Goods goods1 = new Goods();
                    goods1.setGoodsId(returnListGoods.getGoodsId());
                    goods1.setInventoryQuantity(returnListGoods.getGoodsNum());
                    goodss.add(goods1);
                }
                purchaseMapper.saveReturnListGoods(list);
                goodsMapper.cutStockList(goodss);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Map<String, Object> purchaseListGoodsList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<PurchaseList> list = purchaseMapper.searchPurchaseListGoods(purchaseNumber,supplierId,state,sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> purchaseListGoodsGetGoodss(Integer purchaseListId) {
        List<PurchaseListGoods> list  = purchaseMapper.purchaseListGoodsGetGoodss(purchaseListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delPurchaseListGoods(Integer purchaseListId) throws RuntimeException{
        purchaseMapper.delPurchaseListGoods(purchaseListId);
        purchaseMapper.delPurchaseList(purchaseListId);

    }

    @Override
    public Map<String, Object> returnListGoodsList(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        List<ReturnList> list = purchaseMapper.searchReturnListGoods(returnNumber,supplierId,state,sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    public Map<String, Object> returnListGoodsGetGoodss(Integer returnListId) {
        List<ReturnListGoods> list  = purchaseMapper.returnListGoodsGetGoodss(returnListId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delReturnListGoods(Integer returnListId) {
        purchaseMapper.delReturnListGoods(returnListId);
        purchaseMapper.delReturnList(returnListId);
    }

    @Override
    public void updateState(Integer purchaseListId) {
        purchaseMapper.updateState(purchaseListId);
    }

    @Override
    public List<Map> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        List<Map> count = purchaseMapper.count(sTime,eTime,goodsTypeId,codeOrName);
        return count;
    }
}
